﻿namespace Sidvall.Data
{
    public class SaveSettings : CriteriaBase
    {
        #region Public Members

        public SaveMode Mode { get; set; }

        #region GetDataRowState

        public DataRowState GetDataRowState(DataRowState state)
        {
            return GetDataRowState(this.Mode, state);
        }
        public DataRowState GetDataRowState(IDataRowContextProvider item)
        {
            return GetDataRowState(this.Mode, item);
        }

        #endregion
        #region GetDataRowStateResolved

        public DataRowState GetDataRowStateResolved(IDataRowContextProvider item)
        {
            return GetDataRowStateResolved(this.Mode, item);
        }

        #endregion
        #region GetDataRowState - Static

        public static DataRowState GetDataRowState(SaveMode mode, DataRowState state)
        {
            switch (mode)
            {
                case SaveMode.ForceAdd:
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Added;
                    return state;
                case SaveMode.ForceAddForAll:
                case SaveMode.ForceAddForAllKeepKey:
                    return DataRowState.Added;
                case SaveMode.ForceUpdate:
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Modified;
                    return state;
                case SaveMode.ForceUpdateForAll:
                    return DataRowState.Modified;
                default:
                    return state;
            }
        }
        public static DataRowState GetDataRowState(SaveSettings item, DataRowState state)
        {
            if (item == null)
                return GetDataRowState(SaveMode.Default, state);
            return GetDataRowState(item.Mode, state);
        }
        public static DataRowState GetDataRowState(SaveMode mode, IDataRowContextProvider provider)
        {
            DataRowState state;

            switch (mode)
            {
                case SaveMode.ForceAdd:
                    state = DataRowContextManager.GetDataRowState(provider);
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Added;
                    return state;
                case SaveMode.ForceAddForAll:
                case SaveMode.ForceAddForAllKeepKey:
                    return DataRowState.Added;
                case SaveMode.ForceUpdate:
                    state = DataRowContextManager.GetDataRowState(provider);
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Modified;
                    return state;
                case SaveMode.ForceUpdateForAll:
                    return DataRowState.Modified;
                default:
                    return DataRowContextManager.GetDataRowState(provider);
            }
        }
        public static DataRowState GetDataRowState(SaveSettings item, IDataRowContextProvider provider)
        {
            if (item == null)
                return GetDataRowState(SaveMode.Default, provider);
            return GetDataRowState(item.Mode, provider);
        }
        public static DataRowState GetDataRowState(ISaveContext item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");
            return GetDataRowState(item.Settings, item.Item as IDataRowContextProvider);
        }

        #endregion
        #region GetDataRowStateResolved - Static

        public static DataRowState GetDataRowStateResolved(SaveMode mode, IDataRowContextProvider provider)
        {
            DataRowState state;

            switch (mode)
            {
                case SaveMode.ForceAdd:
                    state = DataRowContextManager.GetDataRowStateResolved(provider);
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Added;
                    return state;
                case SaveMode.ForceAddForAll:
                case SaveMode.ForceAddForAllKeepKey:
                    return DataRowState.Added;
                case SaveMode.ForceUpdate:
                    state = DataRowContextManager.GetDataRowStateResolved(provider);
                    if (state == DataRowState.Unchanged)
                        return DataRowState.Modified;
                    return state;
                case SaveMode.ForceUpdateForAll:
                    return DataRowState.Modified;
                default:
                    return DataRowContextManager.GetDataRowStateResolved(provider);
            }
        }
        public static DataRowState GetDataRowStateResolved(SaveSettings item, IDataRowContextProvider provider)
        {
            if (item == null)
                return GetDataRowStateResolved(SaveMode.Default, provider);
            return GetDataRowStateResolved(item.Mode, provider);
        }
        public static DataRowState GetDataRowStateResolved(ISaveContext item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");
            return GetDataRowStateResolved(item.Settings, item.Item as IDataRowContextProvider);
        }

        #endregion
        #region GetKeepKey - Static

        public static bool GetKeepKey(SaveSettings item)
        {
            if (item == null)
                return false;
            return item.KeepKey;
        }

        #endregion
        #region GetOperationResolved - Static

        public static string GetOperationResolved(SaveMode mode, IDataRowContextProvider provider)
        {
            var state = GetDataRowStateResolved(mode, provider);
            return Sidvall.Security.Operations.GetOperation(state);
        }
        public static string GetOperationResolved(SaveSettings item, IDataRowContextProvider provider)
        {
            var state = GetDataRowStateResolved(item, provider);
            return Sidvall.Security.Operations.GetOperation(state);
        }
        public static string GetOperationResolved(ISaveContext item)
        {
            var state = GetDataRowStateResolved(item);
            return Sidvall.Security.Operations.GetOperation(state);
        }

        #endregion
        #region IsSavable - Static

        public static bool IsSavable(SaveMode mode, IDataRowContextProvider provider)
        {
            if (provider == null)
                return false;
            if (mode != Data.SaveMode.Default)
                return true;
            if (provider.DataRowStateIsDeleted)
                return true;
            if (provider.DataRowStateIsNew)
                return true;
            if (provider.DataRowStateIsDirty)
                return true;
            return false;
        }
        public static bool IsSavable(SaveSettings item, IDataRowContextProvider provider)
        {
            if (item == null)
                return IsSavable(SaveMode.Default, provider);
            return IsSavable(item.Mode, provider);
        }
        public static bool IsSavable(ISaveContext item)
        {
            if (item == null)
                return false;
            return IsSavable(item.Settings, item.Item as IDataRowContextProvider);
        }

        #endregion

        #endregion
        #region Private Members

        #region KeepKey

        private bool KeepKey
        {
            get
            {
                switch (this.Mode)
                {
                    case SaveMode.ForceAddForAllKeepKey:
                        return true;
                    default:
                        return false;
                }
            }
        }

        #endregion

        #endregion
        #region Constructors

        public SaveSettings()
            : this(SaveMode.Default)
        {
        }
        public SaveSettings(SaveMode mode)
        {
            this.Mode = mode;
        }

        #endregion
    }
}
